
layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_TCW_FinancesEventGroupsOp';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			t.c.data = c.data; // 数据

			//将表单数据带入js
			t.c.formObj = c.formObj;
			/**自定义的字段，是否流转中**/
			t.c.onProcess = c.onProcess;

			//复选框选中，存放行号
			t.c.checkedIndex = [];
		},
		load:function () { // 加载控件
			var h = ``
				+ `<div class="layui-form layui-form-item" lay-filter="` + this.id + `" style="background-color: #ffffff;">`
				+ `<label style="font-weight: 700;font-size: 18px; padding: 5px;">网店列表</label>`
				+ `</div>`
			if(this.c.onProcess == 0) {
				h += `<div class="layui-table-tool" style="padding: 0; min-height: 0; background-color: #ffffff;">`
					+ `<div class="layui-table-tool-temp">`
					+ `<div class="layui-btn-container">`
					+ `<a class="layui-btn layui-btn-normal layui-btn-sm " lay-event="event-to-select"><i class="iconfont icon-add-btn"></i>新增</a>`
					+ `<a class="layui-btn layui-btn-danger layui-btn-sm " lay-event="event-to-del" ><i class="layui-icon layui-icon-delete">删除</a>`
					+ `</div>`
					+ `</div>`
					+ `</div>`
			}
			h+= `<div class="layui-form layui-form-item" pane>`
				+ `<div id="`+this.c.tableId+`">`
				+ this.loadTable()
				+ `</div>`
				+ `</div>`
				+ `</div>`;
			$('#'+this.c.id).html(h);
			this.listenBtn();
			this.listenBtnByTd();
		},
		loadTable:function () { // 加载表格
			return ``
				+ `<div class="layui-table-box">`
				+ `<div class="layui-table-header" style="height: 300px; max-height: 300px; overflow: auto; background-color: #ffffff;">`
				+ `<table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="grid" lay-even="" style="margin: 0;">`
				+ `<thead>`
				+ this.loadTableOfThead()
				+ `</thead>`
				+ `<tbody>` + this.loadTableOfTbody()+`</tbody>`
				+ `</table>`
				+ `</div>`
				+ `</div>`;
		},
		loadTableOfThead:function () { // 加载表头
			var h = `<thead>`;
			h +=`<tr>`;
			h += `<th style="display: none;">id</th>`;
			if (this.c.onProcess == 0) {
				h += `<th style="width:1.9%; text-align: center; color: #000000;line-height: 18px !important;">
							<input type="checkbox" id="checkAll" lay-skin="primary" lay-filter="checkAll">
						</th>`;
			}
			h += `<th style="width:5%; text-align: center; color: #000000;">网店名称</th>`;
			h += `<th style="width:5%; text-align: center; color: #000000;">运营公司ID</th>`;
			h += `</tr>`;
			h + `</thead>`;
			return h;
		},
		loadTableOfTbody:function () { // 加载表tbody
			var t = this;
			var h = `<tbody id="tbody">`;
			for(var i=0;i<t.c.data.length;i++){
				h += `<tr id="table_th_`+i+`">`;
				h += t.loadTableTdOfTbody(t.c.data[i],i);
				h+= `</tr>`;
			}
			h += `</tbody>`;
			return h;
		},
		loadTableTdOfTbody: function (data,i) { // 加载表tbody
			var h =`<td style="display: none;">` + data.id + `</td>`;
			if (this.c.onProcess == 0) {
				h += `<td style="text-align:center;padding:0;">
					<input type="checkbox" class="chBox"  lay-skin="primary" data-index="` + i + `"  lay-filter="checkBoxOne">
					</td>`;
			}
			h += `<td style="text-align:center;padding:0;">` + data.shopName + `</td>`;
			h += `<td style="text-align:center;padding:0;">` + data.companyID + `</td>`;
			return h;
		},
		listenBtn:function () { // 添加监听
			var t = this;

			// 监听“新增”按钮
			$('a[lay-event="event-to-select"]').on('click',function (e) {
				var selectedData = [];
				//回填数据的代码
				for (var i = 0; i < t.c.data.length; i++) {
					var row = {};
					row.id = t.c.data[i].id;
					row.companyID = t.c.data[i].companyID;
					row.shopName = t.c.data[i].shopName;
					selectedData.push(row);
				}
				layui.YsModal.init({
					modalKey: 'TP_AmazonShopDp',
					type: 'checkbox',
					uniqueKey: 'id',
					parentObj: t,
					selectedData: selectedData
				})
			});

			//全选
			layui.form.on('checkbox(checkAll)', function(data){
				var elem = data.elem;
				$(".chBox").prop("checked", elem.checked);
				var checkedIndex = [];
				$(".chBox").each(function(k, v){
					if ($(v).is(':checked')) {
						checkedIndex.push($(v).attr('data-index'));
					}
				});
				t.c.checkedIndex = checkedIndex;
				layui.form.render();
			});
			// 列表里的小选择框点击
			layui.form.on('checkbox(checkBoxOne)', function(data){
				var elem = data.elem;
				if ($(".chBox").not('input:checked').length) {
					$('#checkAll').prop("checked", false);
				} else {
					$('#checkAll').prop("checked", true);
				}
				var checkedIndex = [];
				$(".chBox").each(function(k, v){
					if ($(v).is(':checked')) {
						checkedIndex.push($(v).attr('data-index'));
					}
				});
				t.c.checkedIndex = checkedIndex;
				layui.form.render();
			});

			// 监听“删除”按钮
			$('a[lay-event="event-to-del"]').on('click',function (e) {
				var checkedIndexList = t.c.checkedIndex;
				if (checkedIndexList.length ==0 || checkedIndexList==null){
					return layer.msg("请至少选择一条要删除的数据!");
				}
				var newData = [];
				for (var i = 0; i < t.c.data.length; i++) {
					var isDel = false;
					for (var j = 0; j < checkedIndexList.length; j++) {
						if(i == checkedIndexList[j]){
							isDel = true;
						}
					}
					if(!isDel){
						newData.push(t.c.data[i]);
					}
				}
				t.c.data = newData;
				$('#' + t.c.id).empty();
				return t.reload(t.c);
			});
		},

		listenBtnByTd:function () { // 添加监听
			var t = this;
			var domTable = $('#' + t.c.tableId);

			// 表单控件重新加载
			form.render();
		},


		removeRow:function(index) { // 重新加载表格
			var t = this;
			t.c.data.splice(index, 1);
			return t.reload(t.c);
		},
		reloadTable:function () { // 重新加载表格
			$('#'+this.c.tableId).html(this.loadTable());
			this.listenBtn();
		},
		getData:function () {
			return this.c.data;
		},
		init:function (c) {this.f(c);this.load();return object;},
		reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
		addRow:function (row) {
			var c = this.c;
			var t = this;
			c.data.push(row);
			var h =  `<tr id="table_th_`+(c.data.length-1)+`">`;
			h += t.loadTableTdOfTbody(row,c.data.length-1);
			h+= `</tr>`;
			$('#tbody').append(h);
			t.listenBtnByTd();
		},
		addRows:function (rows) {
			var c = this.c;
			for (var i = 0; i < rows.length; i++) {
				c.data.push(rows[i]);
			}
			$('#' + c.id).empty();
			return this.reload(c);
		},
		addRowsDx:function (rows) {
			var c = this.c;
			c.data = [];
			for (var i = 0; i < rows.length; i++) {
				c.data.push(rows[i]);
			}
			$('#' + c.id).empty();
			return this.reload(c);
		},
		editRow:function (index, row) {
			var c = this.c;
			c.data[index] = row;
			$('#' + c.id).empty();
			return this.reload(c);
		},
		editRowOfAttr:function (index, row) {
			var c = this.c;
			for(var key in row){
				c.data[index][key] = row[key]
			}
			$('#' + c.id).empty();
			return this.reload(c);
		},
		reloadRows:function (rows) {
			var c = this.c;
			c.data = rows;
			return this.reload(c);
		},

		//多选框
		matchValuesAfterModalSelectCheckbox: function (obj1, obj2, obj3) {
			var t = this;
			var rowCurrents = [];
			for (var i = 0; i < obj1.length; i++) {
				var obj = obj1[i];
				var rowCurrent = {};
				rowCurrent.id = obj.id;
				rowCurrent.companyID = obj.companyID;
				rowCurrent.shopName = obj.shopName;
				rowCurrents.push(rowCurrent);
			}
			t.addRowsDx(rowCurrents);
		},
	};
	exports('simple_table_TCW_FinancesEventGroupsOp', object);
});